<?php

namespace App\Repository\Repositories;


use App\Http\Serve\Express;
use App\Models\StoreCategory;
use App\Models\StoreProduct;
use App\Models\StoreProductRule;
use App\Repository\RepositoryInterface\ProductInterface;
use Illuminate\Support\Facades\DB;
use Mockery\Exception;


class ProductRepositories implements ProductInterface
{
    /**
     * @param $arr
     * @return int|mixed
     * 添加商品的规格
     */
    public function AddMoreSpec($arr)
    {
        // TODO: Implement AddMoreSpec() method.

        $param = $arr;
        $param = $param['spec'];
        $aa = StoreProductRule::where('id', 1)->get();
        $rule_name = array_keys($param);
        $rule_name = array_keys($param);
        $StoreProduce = new StoreProductRule();
        try {
            foreach ($rule_name as $key => $value) {
                $rule[] = json_encode($param[$value], JSON_UNESCAPED_UNICODE);
            }
            foreach ($rule_name as $key => $value) {
                $param = [
                    'rule_name' => $value,
                    'rule_value' => $rule[$key]
                ];
                $res = $StoreProduce->AddRules($param);
            }
            return $res;
        } catch (\Exception $exception) {
            return $exception->getMessage();
        }

    }

    /**
     * 商品列表
     * @return mixed
     */
    public function goodsList()
    {
        return StoreProduct::where('is_del',0)->where('is_show',1)->get();
    }

    /**
     * 商品排行榜
     * @param $id
     * @param $ids
     * @return mixed
     */
    public function goodsRank($id, $ids)
    {
        return StoreProduct::whereIn('id', $id)
            ->orderByRaw(DB::raw("FIELD(id, $ids)"))->get();
    }


    /**
     * 获取分类列表
     * @return mixed
     */
    public function index($where)
    {
        return StoreCategory::where($where)->get();

    }

    public function categoryList()
    {
        return StoreCategory::get();
    }

    /**
     * 数据添加入库
     * @param $data
     * @return mixed
     */
    public function save($data)
    {
        return StoreCategory::create($data);
    }

    /**
     * 修改之前的查询
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {
        return StoreCategory::where('id', $id)->first();
    }

    /**
     * 分类修改
     * @param $id
     * @param $data
     * @return mixed
     */
    public function update($id, $data)
    {
        return StoreCategory::where('id', $id)->update($data);
    }

    /**
     * 商品分类详情
     * @param $id
     * @return mixed
     */
    public function read($id)
    {
        return StoreCategory::where('id', $id)->first();
    }

    /**
     * 商品分类删除
     * @param $id
     * @return mixed
     */
    public function delete($id)
    {
        return StoreCategory::where('id', $id)->delete();
    }

    /**
     * 状态修改
     * @param $id
     * @return mixed
     */
    public function change($id)
    {
        $data = StoreCategory::where('id', $id)->first();
        if ($data->is_show == 1) {
            $data->is_show = 0;
        } else {
            $data->is_show = 1;
        }
        return $data->save();
    }

    public function ProductAdd($arr)
    {
        // TODO: Implement ProductAdd() method.
    }

    /**
     * @return mixed
     * 规格列表
     */
    public function AttrRuleList()
    {
        // TODO: Implement test() method.
        $arr = StoreProductRule::get();
        return $arr;
    }
}
